//
//  MyRadioViewController.m
//  MoGuRadio
//
//  Created by dlios on 15/6/4.
//  Copyright (c) 2015年 mogu.com 蘑菇科技. All rights reserved.
//

#import "MyRadioViewController.h"
#import "BaseViewController.h"
#import "FSAudioStream.h" // 网络播放
#import "UIImageView+AFNetworking.h"

//第三方
#import "UIImageView+AFNetworking.h"

// 自定义类
#import "DataBaseHandler.h"
#import "MushRoom.h"

//引用音乐播放
#import <AVFoundation/AVFoundation.h>

@interface MyRadioViewController () <AVAudioPlayerDelegate, UINavigationControllerDelegate>
{
    CABasicAnimation *imageAni;
}

@property (nonatomic, retain) UIImageView *centerImage;
@property (nonatomic, retain) UIImageView *glassImage;
@property (nonatomic, retain) UILabel *audioNameLabel;
@property (nonatomic, retain) UIButton *addButton; //关注
@property (nonatomic, retain) UIButton *playButton; //播放
@property (nonatomic, retain) UIButton *leftButton; //上一曲
@property (nonatomic, retain) UIButton *rightButton; //下一曲
@property (nonatomic, retain) UIButton *endButton; //暂停

@property (nonatomic, strong) AVAudioPlayer *audioPlayer; //播放器
@property (nonatomic, retain) UIProgressView *playProgress; //进度条
@property (nonatomic, retain) NSTimer *timer; //更新定时器
@property (nonatomic, retain) UIButton *likeButton; //点赞
@property (nonatomic, retain) UIButton *likeButton2;//点赞替换
@property (nonatomic, retain) UIButton *fxButton; //分享

@property (nonatomic, assign) BOOL btnStatus;

@property (nonatomic, assign) BOOL isPause;

@end

static MyRadioViewController *myRadio =nil;

@implementation MyRadioViewController



- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    self.btnStatus = NO;
    self.isPause = NO;
    [self timer];
    [self.audioStream play];
    [self playPage];
    
}

+ (MyRadioViewController *)shareInstance
{
    
    static dispatch_once_t once;
    dispatch_once(&once, ^{
        if (myRadio == nil) {
            myRadio = [[MyRadioViewController alloc] init];
        }
        
    });
    
    return myRadio;
}


- (void)playPage
{
    //设置毛玻璃
    UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, VIEW_WIDTH, VIEW_HEIGHT)];
    image.image = [UIImage imageNamed:@"下部分.png"];
    [self.view addSubview:image];
    [image release];
    
    
    //电台节目名称显示
    self.audioNameLabel = [[UILabel alloc] init];
    self.audioNameLabel.frame = CGRectMake(VIEW_WIDTH / 8, VIEW_HEIGHT / 55, VIEW_HEIGHT / 2, VIEW_HEIGHT / 7);
    self.audioNameLabel.text = [NSString stringWithFormat:@"电台节目: %@", self.rname];
    self.audioNameLabel.textColor = [UIColor whiteColor];
    self.audioNameLabel.font = [UIFont boldSystemFontOfSize:18];
    self.audioNameLabel.alpha = 0.5;
    [self.view addSubview:self.audioNameLabel];
    [_audioNameLabel release];
    //适应行数
    self.audioNameLabel.numberOfLines = 0;
    

    
    //中间圆形视图


    self.centerImage = [[UIImageView alloc] initWithFrame:CGRectMake(VIEW_WIDTH / 3.8, VIEW_HEIGHT / 7, VIEW_WIDTH / 2.1, VIEW_HEIGHT / 3.1)];

//    self.centerImage = [[UIImageView alloc] initWithFrame:CGRectMake(VIEW_WIDTH / 3.8, VIEW_HEIGHT / 7, VIEW_WIDTH / 2.1, VIEW_HEIGHT / 3.1)];


    self.centerImage = [[UIImageView alloc] initWithFrame:CGRectMake(VIEW_WIDTH / 4, VIEW_HEIGHT / 6, VIEW_WIDTH / 2.2, VIEW_HEIGHT / 3.7)];

    self.centerImage.backgroundColor = [UIColor colorWithRed:0.3 green:0.5 blue:0.47 alpha:0.5];
    NSString *centerImage = self.playImage;
    [self.centerImage setImageWithURL:[NSURL URLWithString:centerImage]];
    self.centerImage.layer.masksToBounds = YES;
    
    
    
    //圆角设置
    
    self.centerImage.layer.cornerRadius = 20;
    
    self.centerImage.layer.masksToBounds = YES;

    [self.centerImage.layer addAnimation:imageAni forKey:@""];
    [self.view addSubview:self.centerImage];
    //需网络传值
    [_centerImage release];
    
    
    
    //收藏这个节目按钮
    self.addButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    self.addButton.backgroundColor = [UIColor clearColor];
    self.addButton.frame = CGRectMake(VIEW_WIDTH / 7, VIEW_HEIGHT / 2.07, VIEW_WIDTH / 1.5, VIEW_HEIGHT / 15);
    self.addButton.backgroundColor = [UIColor clearColor];
    self.addButton.titleLabel.font = [UIFont boldSystemFontOfSize:15];
    [self.addButton.layer setCornerRadius:5.0]; //设置矩形四个圆角半径
    [self.addButton.layer setBorderWidth:0.6]; //边框宽度
    [self.addButton.layer setBorderColor:[UIColor whiteColor].CGColor];// 设置Button边框颜色
   
    [self.addButton setTitle:@"收藏这个节目" forState:UIControlStateNormal];//button title
    
    self.addButton.tintColor = [UIColor whiteColor];
    [self.view addSubview:self.addButton];
    
    
    //播放按钮
    self.playButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];


    self.playButton.frame = CGRectMake(VIEW_WIDTH / 2.1, VIEW_HEIGHT / 1.6, VIEW_WIDTH / 20, VIEW_HEIGHT / 20);

    self.playButton.frame = CGRectMake(VIEW_WIDTH / 2, VIEW_HEIGHT / 1.5, VIEW_WIDTH / 12, VIEW_HEIGHT / 19);

    //self.playButton.frame = CGRectMake(VIEW_WIDTH / 1.8, VIEW_HEIGHT / 1.6, VIEW_WIDTH / 20, VIEW_HEIGHT / 20);

    self.playButton.frame = CGRectMake(VIEW_WIDTH / 2.15, VIEW_HEIGHT / 1.5, VIEW_WIDTH / 12, VIEW_HEIGHT / 19);


    UIImage *playImage = [UIImage imageNamed:@"pause1.png"];
    playImage = [playImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    [self.playButton setImage:playImage forState:UIControlStateNormal];
    [self.view addSubview:self.playButton];
    //需要播放方法
    [self.playButton addTarget:self action:@selector(playButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    
    //进度条
<<<<<<< HEAD

    self.playProgress = [[UIProgressView alloc] initWithFrame:CGRectMake(VIEW_WIDTH / 20, VIEW_HEIGHT / 1.7, VIEW_WIDTH / 1.1, VIEW_HEIGHT / 0.5)];
    self.playProgress.alpha = 0.7;
    [self.view addSubview:self.playProgress];
=======
//    self.playProgress = [[UIProgressView alloc] initWithFrame:CGRectMake(VIEW_WIDTH / 20, VIEW_HEIGHT / 1.7, VIEW_WIDTH / 1.1, VIEW_HEIGHT / 0.5)];
//    self.playProgress.alpha = 0.7;
//    [self.view addSubview:self.playProgress];
    
>>>>>>> c1f2daaa683b676696d15cbdc25e38c446ffa905

//    
//    //关注这个节目按钮
//    self.addButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
//    self.addButton.backgroundColor = [UIColor clearColor];
//    self.addButton.frame = CGRectMake(90, 330, 200, 40);
//    self.addButton.backgroundColor = [UIColor clearColor];
//    [self.addButton.layer setCornerRadius:5.0]; //设置矩形四个圆角半径
//    [self.addButton.layer setBorderWidth:0.6]; //边框宽度
//    [self.addButton setTitle:@"收藏这个节目 0 w 0" forState:UIControlStateNormal];//button title
    
    

//
//    //上一曲按钮(左)
//    self.leftButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
//    self.leftButton.frame = CGRectMake(50, 430, 35, 35);
//    UIImage *lastImage = [UIImage imageNamed:@"last1.png"];
//    lastImage = [lastImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
//    [self.leftButton setImage:lastImage forState:UIControlStateNormal];
//    [self.view addSubview:self.leftButton];
//    
//    
//    //下一曲按钮(右)
//    self.rightButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
//    self.rightButton.frame = CGRectMake(280, 430, 35, 35);
//    UIImage *nextImage = [UIImage imageNamed:@"next1.png"];
//    nextImage = [nextImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
//    [self.rightButton setImage:nextImage forState:UIControlStateNormal];
//    [self.view addSubview:self.rightButton];
    
<<<<<<< HEAD
=======
    
>>>>>>> c1f2daaa683b676696d15cbdc25e38c446ffa905

    //第三方分享 >>
    self.fxButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    self.fxButton.frame = CGRectMake(VIEW_WIDTH / 6, VIEW_HEIGHT / 1.6, VIEW_WIDTH / 20, VIEW_HEIGHT / 25);

    self.playProgress = [[UIProgressView alloc] initWithFrame:CGRectMake(VIEW_WIDTH / 22, VIEW_HEIGHT / 1.65, VIEW_WIDTH / 1.1, VIEW_HEIGHT / 30)];
    self.playProgress.alpha = 0.9;
    [self.view addSubview:self.playProgress];


<<<<<<< HEAD
    
=======
>>>>>>> c1f2daaa683b676696d15cbdc25e38c446ffa905

    
    //第三方分享 >>
    self.fxButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];

    self.fxButton.frame = CGRectMake(VIEW_WIDTH / 5 , VIEW_HEIGHT / 1.5, VIEW_WIDTH / 12, VIEW_HEIGHT / 19);

    self.fxButton.frame = CGRectMake(VIEW_WIDTH / 7 , VIEW_HEIGHT / 1.5, VIEW_WIDTH / 12, VIEW_HEIGHT / 19);


    UIImage *shareImage = [UIImage imageNamed:@"sharekk.png"];
    shareImage = [shareImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    [self.fxButton setImage:shareImage forState:UIControlStateNormal];
    [self.fxButton addTarget:self action:@selector(shareWeiBoButtonAcion:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.fxButton];
    
    

    // 点赞
    self.likeButton2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];


    self.likeButton2.frame = CGRectMake(VIEW_WIDTH / 1.25, VIEW_HEIGHT / 1.6, VIEW_WIDTH / 20, VIEW_HEIGHT / 25);

    self.likeButton2.frame = CGRectMake(VIEW_WIDTH / 1.2, VIEW_HEIGHT / 1.5, VIEW_WIDTH / 12, VIEW_HEIGHT / 19);

   // self.likeButton2.frame = CGRectMake(VIEW_WIDTH / 0.5, VIEW_HEIGHT / 1.6, VIEW_WIDTH / 20, VIEW_HEIGHT / 25);

    self.likeButton2.frame = CGRectMake(VIEW_WIDTH / 1.27, VIEW_HEIGHT / 1.5, VIEW_WIDTH / 12, VIEW_HEIGHT / 19);


    UIImage *likeafterImage = [UIImage imageNamed:@"likebefore.png"];
    likeafterImage = [likeafterImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    [self.likeButton2 setImage:likeafterImage forState:UIControlStateNormal];
    [self.likeButton2 addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.likeButton2];
    
}


// 播放进度条控制器
- (NSTimer *)timer
{
    if (!_timer) {
        
        _timer = [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(updateProgress) userInfo:nil repeats:true];
    }
    
    return _timer;
    
}

// 控制当前播放的位置
- (void)updateProgress
{
    float progress = self.audioStream.currentTimePlayed.position;
    [self.playProgress setProgress:progress animated:true];
}


- (void)click:(id)sender
{
    if (self.btnStatus == YES) {
        
         [self.likeButton2 setBackgroundImage:[UIImage imageNamed:@"likebefore.png"] forState:UIControlStateNormal];
        
    } else {
        
       [self.likeButton setBackgroundImage:[UIImage imageNamed:@"likeafter.png"] forState:UIControlStateNormal];
        
    }
   self.btnStatus = !self.btnStatus;
}

// 播放页面设置成单例以后,需要重新set方法来实现赋值
- (void)setMp3Url:(NSString *)mp3Url
{
    if (_mp3Url != mp3Url) {
        [_mp3Url release];
        _mp3Url = [mp3Url retain];
    }
    NSURL *url = [NSURL URLWithString:self.mp3Url];
    [self.audioStream playFromURL:url];
}

-(void)setPlayImage:(NSString *)playImage
{
    if (_playImage != playImage) {
        
        [_playImage release];
        _playImage = [playImage retain];
    }
    [self.centerImage setImageWithURL:[NSURL URLWithString:self.playImage]];
}

- (void)setRname:(NSString *)rname
{
    
    if (_rname != rname) {
        
        [_rname release];
        _rname = [rname retain];
    }

    self.audioNameLabel.text = [NSString stringWithFormat:@"电台节目: %@", self.rname];
}


//url播放
- (NSURL *) getNetworkUrl{

    NSString *urlStr = self.mp3Url;
    NSURL *url = [NSURL URLWithString:urlStr];
    return url;
}

-(FSAudioStream *)audioStream

{
    if (!_audioStream) {
        NSURL *url = [self getNetworkUrl];
        self.audioStream = [[FSAudioStream alloc] initWithUrl:url];
        self.audioStream.onFailure = ^(FSAudioStreamError error, NSString *description){
            NSLog(@"播放过程中发生错误, 错误信息:%@", description);
        };
        self.audioStream.onCompletion = ^(){
            NSLog(@"播放完成!!");
        };
        [self.audioStream setVolume:0.5];
    }

    return  _audioStream;
}


- (void)playButtonAction:(UIButton *)sender
{
    if(_btnStatus){
        
        UIImage *playImage = [UIImage imageNamed:@"pause1.png"];
        playImage = [playImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        [sender setImage:playImage forState:UIControlStateNormal];
        [sender setImage:playImage forState:UIControlStateHighlighted];

        if (self.isPause) {
            
            [self.audioStream pause];
            
        } else {
        
            [self.audioStream play];
        }
        self.isPause = NO;

    } else {
        
        UIImage *pauseImage = [UIImage imageNamed:@"play123.png"];
        pauseImage = [pauseImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        [sender setImage:pauseImage forState:UIControlStateNormal];
        [sender setImage:pauseImage forState:UIControlStateHighlighted];
        [self.audioStream pause];
        self.isPause = YES;
    }
    _btnStatus = !_btnStatus;
}


- (void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBar.translucent = NO;
    self.tabBarController.tabBar.hidden = YES;
    
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"上部分.png"] forBarMetrics:UIBarMetricsDefault];
    self.navigationController.navigationBar.barStyle = UIBarStyleBlackTranslucent;
}


- (void)viewWillDisappear:(BOOL)animated
{
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@""] forBarMetrics:UIBarMetricsDefault];
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:0.097 green:0.6701 blue:0.4134 alpha:1.0];
}


- (void)dealloc
{
    [_audioNameLabel release];
    [_centerImage release];
    [_glassImage release];
    [_addButton release];
    [_playButton release];
    [_leftButton release];
    [_rightButton release];
    [_endButton release];
    [_audioPlayer release];
    [_playProgress release];
    [_likeButton release];
    [_fxButton release];
    [super dealloc];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
